
/**
 *	TrueMusic - simple java mp3 player
 *
 *	TrueMusic is free software; you can redistribute it
 *	and/or modify it under the terms of the GNU General
 *	Public License as published by the Free Software
 *	Foundation; either version 2 of the License or (at
 *	your option) any later version.
 *
 *	This program is distributed in the hope that it will be
 *	useful but WITHOUT ANY WARRANTY; without even the
 *	implied warranty of the MERCHANTABILITY or FITNESS FOR
 *	A PARTICULAR PURPOSE. See the GNU General Public
 *	License for more details.
 *
 *	Copyright (c)2005-2007 Ryan Macnak <rmacnak@gmail.com>
 */

package com.servequake.truemusic;


import javazoom.jl.player.advanced.AdvancedPlayer;
import org.farng.mp3.*;
import java.io.*;

/**
 * JavaLayerAudio plus mp3 tag reading.  Supports regular and vbr mp3.
 */
public class Mp3Audio extends JavaLayerAudio //implements TaggableAudio
{
	String title="";
	String artist="";
	String album="";
	String track="";
	String genre = "";
	String lyrics="";

	boolean fresh=true;
	
	public void load(String res, PlaybackListener l) throws Exception
	{
		super.load(res,l);
		//readTag();
	}
	
	public synchronized void play()
	{
		if(fresh)
		{
			fresh = false;
			try{
			readTag();}catch(Exception e){}
		}
		super.play();
		
	}

	public void readTag() throws Exception
	{
		MP3File f = new MP3File( new File(getResource()), false );
		AbstractMP3Tag tag = null;

		if(f.hasID3v1Tag()) tag = f.getID3v1Tag();
		if(f.hasID3v2Tag()) tag = f.getID3v2Tag();

		if(tag!=null)
		{
			this.title = tag.getSongTitle();
			this.artist = tag.getLeadArtist();
			this.album = tag.getAlbumTitle();
			this.track = tag.getTrackNumberOnAlbum();
			this.genre = tag.getSongGenre();
			this.lyrics = tag.getSongLyric();
		}
	}

	public void writeTag(String title, String artist, String album, String track, String genre, String lyrics) throws Exception
	{
		MP3File f = new MP3File( new File(getResource()), false );
		AbstractMP3Tag tag = null;

		if(f.hasID3v1Tag()) tag = f.getID3v1Tag();
		if(f.hasID3v2Tag()) tag = f.getID3v2Tag();

		tag.setSongTitle( title );
		tag.setLeadArtist( artist );
		tag.setAlbumTitle( album );
		tag.setTrackNumberOnAlbum( track );
		tag.setSongGenre( genre );
		tag.setSongLyric( lyrics );

		f.save(org.farng.mp3.TagConstant.MP3_FILE_SAVE_OVERWRITE);

		readTag();
	}

	public String getTitle()
	{
		if(title.equals("")) return super.getTitle();
		return title;
	}

	public String getArtist(){return artist;}
	public String getAlbum(){return album;}
	public String getTrack(){return track;}
	public String getGenre() { return genre; }	
	public String getLyrics() {	return lyrics; }

	public String toString()
	{
		if(title.equals("")) return super.toString();
		return "\""+title +"\" by "+artist;
	}

	public String formatName(){return "mp3";}
	public String[] getExtensions(){return new String[]{".mp3"};}
}
